import { _getBuddyInfoList, getaddress } from "@/api/IM/index.js";
// 好友管理器
const requestinfo = {
  state: {
    friendList: [],
    friendListTotal: -1
  },
  mutations: {
    CLEAN_USER_LIST: (state) => {
      state.friendList = []
      state.friendListTotal = -1
    },
    GET_USER_LIST: (state, res) => {
      state.friendList = []
      state.tempFriendList = [...state.friendList, ...res.rows]
      state.friendListTotal = res.total
      state.friendList = group(state.tempFriendList, "buddyNameLetter");
    },
  },
  actions: {
    // 获取好友列表
    getUserList({ commit, state }, playod) {
      if (playod.isSearch) {
        commit('CLEAN_USER_LIST')
      }
      _getBuddyInfoList(playod.queryParams).then((res) => {
        if (res.code != 200) {
          return console.log('获取好友列表失败');
        }
        res.rows.forEach((item) => {
          item.avatar =
            item.avatar != ""
              ? "/dev-api" + item.avatar
              : require("../../assets/image/author.jpg");
          item.title = item.nickName;
        });
        commit('GET_USER_LIST', res)
      });
    },
  }
}
function group(arr, key) {
  var map = {}, dest = [];
  for (var i = 0; i < arr.length; i++) {
    var ai = arr[i];
    if (!map[ai[key]]) {
      dest.push({
        key: ai[key],
        data: [ai]
      });
      map[ai[key]] = ai;
    } else {
      for (var j = 0; j < dest.length; j++) {
        var dj = dest[j];
        if (dj.key == ai[key]) {
          dj.data.push(ai);
          break;
        }
      }
    }
  }
  return dest;
}

export default requestinfo